﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LazyKex.Semantic
{
    public class SemanticLazyKex
    {
        private bool isIota = false;
        private CodeGenerator codeGenerator;
        private Stack<int> iotasExpressionCountStack = new Stack<int>();

        public SemanticLazyKex(CodeGenerator codeGenerator)
        {
            this.codeGenerator = codeGenerator;
        }


        public void NormalCharacterAction(char c)
        {
            codeGenerator.GenerateEnterChar(c);
        }

        

        

        public void NewScope()
        {
            codeGenerator.GenerateNewEscope();
        }

        public void EndScope()
        {
            codeGenerator.GenerateEndEscope();
        }

        public void iCharacterAction()
        {
            if (!isIota)
            {
                codeGenerator.GenerateEnterChar('I');
            }
            else
            {
                // i Iota eh T
                codeGenerator.GenerateEnterChar('T');
            }
        }
        public void Q3Action(char c)
        {
            if (c == '*')
            {
                isIota = true;
                // coloca um novo int, indicando o começo de uma iota expression
                iotasExpressionCountStack.Push(0);
            }
            codeGenerator.GenerateEnterChar(c);
        }
        public void EndExpression()
        {
            if (isIota)
            {
                int contaAtual = iotasExpressionCountStack.Pop();
                contaAtual++;
                if (contaAtual == 1)
                {
                    iotasExpressionCountStack.Push(1);
                }
                else if (contaAtual == 2)
                {
                    // se a pilha estiver vazia, saimos do estado iota
                    if (iotasExpressionCountStack.Count == 0)
                        isIota = false;
                }
            }
        }
        

    }
}
